
cap program drop distance_matrix_groupwise
program define distance_matrix_groupwise
	graph drop _all
	set type double
	/* biddata+firmIDdata読み込み*/
	use "../data/biddata020119.dta", clear
	merge 1:1 rid using "../data/rid_address_license_firmID_020119.dta"
	drop _merge
	/* data period 2015-2018*/
	keep if fy_con > 2014 & fy_con < 2018
	/* fill brank firm address and license no.*/ 
	replace ad_firm = ad_firm_new if ad_firm=="" & ad_firm_new ~= ""
	replace no_license = no_license_new if no_license == . & no_license_new < .
	drop *_new
	drop tend_date cont_date
	drop second_round rank1_q rank1
	/* recounstruct regionfirmTG based on bigger sample*/
	egen regionfirmTG = tag(region firmID)


	/* partic,mintsをfirmtestsから取得*/
	merge 1:1 rid region using "../firmleveltest/test_sample_with_results.dta"
	*merge 1:1 rid region using "../firmleveltest24Apr25/test_sample_with_results.dta"
	drop regionfirmID - ftt_hr2p rank1_q - neg_ftt_ts1s tsrank
	*partic,mints1のどっとを埋める
	sort region firmID partic
	by region firmID: replace partic = partic[_n-1] if partic[_n-1] < . & _n > 1
	by region firmID: replace mints1 = mints1[_n-1] if mints1[_n-1] < . & _n > 1
	*format %40s ad* nm*
	*br tsrank fy_con nm_firm ad_firm* no_license* firm*ID
	drop _merge

	/* 242 firms */
	merge m:1 firmID region using "../data/ring_list020119.dta"
	*merge m:1 firmID region using "../firmleveltest24Apr25/ring_list020119.dta"
	drop _merge


	merge m:1 firmID region using "../firmleveltest/ringfirms_plus.dta"
	*merge m:1 firmID region using "../firmleveltest24Apr25/ringfirms_plus.dta"
	drop c1 - c1465
	rename g* g*_plus
	compress
	drop _merge

	*drop c1456 - c1537
	drop g20_plus - g27_plus
	drop g29_plus - gw_distance34_

	drop if g28_plus == 21 | g28_plus == 24
	
	replace g28_plus = 21 if g28_plus == 22
	replace g28_plus = 22 if g28_plus == 23
	replace g28_plus = 23 if g28_plus == 25
	replace g28_plus = 24 if g28_plus == 26
	replace g28_plus = 25 if g28_plus == 27
	replace g28_plus = 26 if g28_plus == 28
	
	
	/* assigning pair id form 1 to 13*/
	*北海道
	gen pair13 = 1 if g28_plus == 1 | g28_plus == 7 		/*A: 道央南（14）,紋別・網走	31*/	
	replace pair13 = 2 if g28_plus == 6 | g28_plus == 8 	/*B: 釧路・根室	14,小樽・ニセコ	14*/
	replace pair13 = 3 if g28_plus == 2 | g28_plus == 3 	/*C: 道央北	8,函館松前	10*/
	replace pair13 = 4 if g28_plus == 4 | g28_plus == 5 	/*D: 留萌	4,稚内	4*/
	*造園
	replace pair13 = 5 if g28_plus == 9 | g28_plus == 14 	/*E: 茨城造園	10,大分造園	5*/
	replace pair13 = 6 if g28_plus == 24 /*26*/ | g28_plus == 25 /*27*/ 	/*F: 福井造園	1,釧路造園	3*/
	*西日本道路
	replace pair13 = 7 if g28_plus == 12 | g28_plus == 21 /*22*/ 	/*G: 島根道路	3,愛媛道路	4*/
	replace pair13 = 8 if g28_plus == 18 | g28_plus == 23 /*25 */	/*H: 岐阜標識	2,四国道路	1*/

	*あまり、ringirmsの数の多い順に割り振り020319
	replace pair13 = 9 if g28_plus == 10 | g28_plus == 16 	/*I: 山形	32,静岡三島	24,数が多い*/
	replace pair13 = 10 if g28_plus == 11 | g28_plus == 19 	/*J: 秋田	11,愛知・岐阜	18*/
	replace pair13 = 11 if g28_plus == 17 | g28_plus == 20 	/*K: 岐阜・長野	8,静岡菊川	6*/
	replace pair13 = 12 if g28_plus == 22 /*23*/ | g28_plus == 26 /*28*/ 	/*L: 青森	3,徳島	6*/
	replace pair13 = 13 if g28_plus == 13 | g28_plus == 15 	/*M: 岐阜電気	2,新潟	2*/
	*0101100011111

	gen group_feature = "Hokkaido" if pair13 > 0 & pair13 <= 4
	replace group_feature = "Landscaping" if pair13 ==5 | pair13 == 6
	replace group_feature = "Road" if pair13 == 7 | pair13 == 8
	replace group_feature = "Others" if pair13 >= 9 & pair13 < .

	gen group_description = "道央南" if g28_p == 1
	replace group_description = "道央北" if g28_p == 2
	replace group_description = "函館松前" if g28_p == 3
	replace group_description = "留萌" if g28_p == 4
	replace group_description = "稚内" if g28_p == 5
	replace group_description = "釧路・根室" if g28_p == 6
	replace group_description = "紋別・網走" if g28_p == 7
	replace group_description = "小樽・ニセコ" if g28_p == 8
	replace group_description = "茨城造園" if g28_p == 9
	replace group_description = "山形" if g28_p == 10
	replace group_description = "秋田" if g28_p == 11
	replace group_description = "島根道路" if g28_p == 12
	replace group_description = "岐阜電気" if g28_p == 13
	replace group_description = "大分造園" if g28_p == 14
	replace group_description = "新潟" if g28_p == 15
	replace group_description = "静岡三島" if g28_p == 16
	replace group_description = "岐阜・長野" if g28_p == 17
	replace group_description = "岐阜標識" if g28_p == 18
	replace group_description = "愛知・岐阜" if g28_p == 19
	replace group_description = "静岡菊川" if g28_p == 20
	replace group_description = "愛媛道路" if g28_p == 21 /*22*/
	replace group_description = "青森" if g28_p == 22 /*23*/
	replace group_description = "四国道路" if g28_p == 23 /*25*/
	replace group_description = "福井造園" if g28_p == 24 /*26*/
	replace group_description = "釧路造園" if g28_p == 25 /*27*/
	replace group_description = "徳島" if g28_p == 26 /*28*/

	gen g28_plus_str = strofreal(g28_plus)
	gen treatment = ustrregexm(g28_plus_str, "^(?:1|2|5|8|12|14|15|16|18|19|20|24|26)$")
	replace g28_plus_str = g28_plus_str + "*" if treatment == 1
	tab g28_plus if treatment == 1
	*	所属が1社ずつしかいない21と24（21には赤木工業（株）_奈良県大和郡山市今国府町２８８－１、24に森下建設（株）_島根県江津市桜江町市山５４３―１）は対象から除外した

	*drop if g28_plus == 21 | g28_plus == 24

	cap drop area
	gen area = "Hokkaido" if g28_plus < 9 | g28_plus == 25
	replace area = "Eastern_Japan" if ((g28_plus >= 9) & (g28_plus <= 11))|((g28_plus >= 15) & (g28_plus <=20))|(g28_plus == 22)
	replace area = "Western_Japan" if area == "" & g28_plus < . /*& g28_plus != 21 & g28_plus != 24*/


	pause on
	foreach pattern in "" /*"plus"*/ {
		preserve
			di "`pattern'"
			if "`pattern'" == ""{
				keep if ring_firm011419 == 1
			}

			* create matrix
			bys g28_plus: gen firm_n28 = _n
			bys g28_plus: gen firm_N28 = _N
			sort g28_plus
			egen firmIDX28 = group(g28_plus)
			
			egen g28_plus_TG = tag(id g28_plus)
			keep if g28_plus_TG == 1
			keep tsrank firmIDX28 g28_plus regionfirmID firm_n28 id nm_firm ///
				 ring_firm  group_description group_feature pair13 treatment area g28_plus_str
			*save "../firmleveltest24Apr25/firmIDX28_map.dta", replace

			*use "../firmleveltest24Apr25/firmIDX28_map.dta", clear
			keep firmIDX28 g28_plus firm_n id group_description group_feature treatment pair13 area g28_plus_str
			sleep 1000
			save "../firmleveltest/freq_matrix28`pattern'.dta", empty replace

			/* 対戦回数のマトリクスをつくる; 対角成分はそのbidderの入札参加回数（自分との対戦回数）となる*/
			gen firmIDX28X = firmIDX28
			*gen firmIDXX = firmIDX

			qui sum firmIDX28
			local rmax = `r(max)'
			forvalues x = 1(1)`rmax'{
				*local x = 1
				cap drop theauctions
				qui count
				di as res "# Round `x'/`rmax'; obs. left:`r(N)'"
				*local x = 1
				bys id: egen theauctions = max( firmIDX28X == `x' )
				qui bys firmIDX28X: egen col`x' = total( theauctions ) if firmIDX28X ~= .
				/* 早く計算するため、処理が終わった企業は1行だけ残してdropする->
					これから、下三角行列ができる->mataのmakesymmetric(X)で対称行列にする*/
					qui drop if firm_n ~= 1 & firmIDX28 == `x'
					*di "AAA"
					qui replace firmIDX28X = . if firmIDX28 == `x'
					drop theauctions
				*qui sort firmIDX
				if (mod(`x', 10) == 0 ){
					sleep 1500
					qui compress
					save "../firmleveltest/freq_matrix28`pattern'.dta", replace
				}
			}
			keep if firm_n28 == 1 
			order col*, seq
			compress
			order firm*
			sort g28_plus

			drop firmIDX28X firm_n28
			sleep 1000
			qui save "../firmleveltest/freq_matrix28`pattern'.dta", replace
			foreach area in "Hokkaido" "Eastern_Japan" "Western_Japan"{
				use "../firmleveltest/freq_matrix28`pattern'.dta", clear
				keep if area == "`area'"
				local colnames = ""
				forvalues x = 1(1)26{
					qui count if firmIDX28 == `x'
					if `r(N)' == 0{
						cap drop col`x'
					}
					else{
						rename col`x' Group`x'
						if ustrregexm("`x'", "^(?:1|2|5|8|12|14|15|16|18|19|20|24|26)$"){
							local colnames = "`colnames' Gr`x'*"
						}
						else{
							local colnames = "`colnames' Gr`x'"  
						}
						di "`colnames'"
					}
				}
				qui ds Group*
				mkmat `r(varlist)', matrix(w) rowprefix("Group") rownames(g28_plus_str)
				mat colnames w = `colnames'
				esttab matrix(w) using ../firmleveltest/freq_matrix28`pattern'_`area', ///
				tex replace nomtitles /*title("`area'")*/ nofloat
			}
		restore
	}
end